<template>
	<!-- 
	swiper中的transfrom会使fixed失效,此时用height="100%"固定高度; 
	swiper中无法触发mescroll-mixins.js的onPageScroll和onReachBottom方法,只能用mescroll-uni,不能用mescroll-body
	-->
	<mescroll-uni ref="mescrollRef" @init="mescrollInit" height="100%" top="60" :down="downOption" @down="downCallback"
		:up="upOption" @up="upCallback">
		<!-- 数据列表 -->
		<good-list :list="goods"></good-list>
	</mescroll-uni>

</template>

<script>

	import MescrollBody from "@/components/mescroll-uni/mescroll-body.vue"
	import MescrollUni from "@/components/mescroll-uni/mescroll-uni.vue"
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import MescrollMoreItemMixin from "@/components/mescroll-uni/mixins/mescroll-more-item.js";
	import {apiSearch} from "@/api/mock.js";
	import {openComDB,executeSQL,dropSQL} from "@/pages/API/sqllite/sqllite.js";
	
	var self;
	export default {
		mixins: [MescrollMixin, MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
		components: {
			MescrollBody,
			MescrollUni
		},
		data() {
			return {
				downOption: {
					auto: false, // 不自动加载 (mixin已处理第一个tab触发downCallback)
					native: false,
					beforeEndDownScroll: 1000,
					textInOffset: "下拉刷新",
					textOutOffset: "释放刷新",
					textLoading: "加载中..."
				},
				upOption: {
					auto: false, // 不自动加载
					 page: {
					 	num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
					 	size: 10 // 每页数据的数量
					 },
					noMoreSize: 2, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty: {
						tip: '~ 暂无数据 ~'
					},
					src: "../../static/images/totop.png",
					offset: 50,
					duration: 300,
					zIndex: 9990,
					right: 20,
					bottom: 120,
					safearea: false,
					width: 72,
					radius: "50%",
					left: null
				},
				goods: [] //列表数据
			}
		},
		props: {
			i: Number, // 每个tab页的专属下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			index: { // 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
				type: Number,
				default () {
					return 0
				}
			},
			tabs: { // 为了请求数据,演示用,可根据自己的项目判断是否要传
				type: Array,
				default () {
					return []
				}
			}
		},
		methods: {
			_aaaaCCCC() {
				this.mescroll.resetUpScroll();
				this.goods=[];
			},
			/*下拉刷新的回调 */
			downCallback() {
				// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
				// loadSwiper();
				// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
				this.mescroll.resetUpScroll();
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				console.log("请求次数");
				//联网加载数据
				self = this;
			//	console.log(this.i);
				let keyword = this.tabs[this.i].name;
				let keyindex = 0;
				//console.log(keyword);
				if (this.i == 0) {
					keyword = "未盘";
					keyindex = 0;
				} else if (this.i == 1) {
					keyword = "正常";
					keyindex = 1;
				} else if (this.i == 2) {
					keyword = "异常";
					keyindex = 2;
				} else if (this.i == 3) {
					keyword = "盘盈";
					keyindex = 3;
				} else if (this.i == 4) {
					keyword = "盘亏";
					keyindex = 4;
				}
				console.log("请求次数1");
				let StockDocument = uni.getStorageSync("StockDocument");
				let documet=JSON.parse(StockDocument);
				let userInfo = uni.getStorageSync("userInfo");
				let querydata={
						"pageSize": page.size,
						"current": page.num,
						manageUser:JSON.parse(userInfo).id,
						queryDocumentIds:[documet.id],
						checkResult:keyindex,
					};
				console.log(querydata);
				const mydataser = uni.getStorageSync("DetailSearch");
				if (mydataser == undefined) {
					mydataser = "";
				}
				if (mydataser != "") {
					
					//sql2+=" and 资产编码 like '%"+mydataser+"%'";
					querydata["assetCode"]=mydataser;
				}
				const mydataname = uni.getStorageSync("DetailName");
				if (mydataname == undefined) {
					mydataname = "";
				}
				if (mydataname != "") {
					
					querydata["assetName"]=mydataname;
				}
				const mydatazclb = uni.getStorageSync("DetailZCLB");
				if (mydatazclb == undefined) {
					mydatazclb = null;
				}
				
				console.log("资产类别"+mydatazclb);
				if (mydatazclb != "" && mydatazclb!='请选择资产类别 >'  && mydatazclb!='请选择资产类别') {
					//mydataservalue["AssetTypeId"]=mydatazclb;
					//sql2+=" and 资产类别 like '%"+mydatazclb+"%'";
					querydata["assetCategory"]=mydatazclb;
				}
				const mydatazcwz = uni.getStorageSync("DetailZCWZ");
				if (mydatazcwz == undefined) {
					mydatazcwz = null;
				}
				if (mydatazcwz != "" && mydatazcwz!=null  && mydatazcwz!='请选择资产位置 >'  && mydatazclb!='请选择资产位置') {
					//mydataservalue["StorePlaceId"]=mydatazcwz;
					//sql2+=" and 资产位置 like '%"+mydatazcwz+"%'";
					querydata["assetPosition"]=mydatazcwz;
				}
				const mydatasybm = uni.getStorageSync("DetailSYBM");
				if (mydatasybm == undefined) {
					mydatasybm = null;
				}
				if (mydatasybm != "" && mydatasybm!=null  && mydatasybm!='请选择使用部门 >' && mydatasybm!='请选择使用部门') {
					querydata["usingOrg"]=mydatasybm;
				}
				const mydatasyr = uni.getStorageSync("DetailSYR");
				if (mydatasyr == undefined) {
					mydatasyr = null;
				}
				if (mydatasyr != "" && mydatasyr!=null  && mydatasyr!='请选择使用人 >' && mydatasyr!='请选择使用人') {
					querydata["usingUser"]=mydatasyr;
				}
				var mydata = this.tabs[this.i].code;
				this.requeststo_tool({
					url:'/app/v1/assetCheck/getCheckDetailPageData',
					type:'POST', 
					data:querydata
				}).then(res2 => {
					console.log(res2);
					
					//convertCheckDocumentList、盘点单信息
						//formatAssetCheckDetailMap  盘点单明细信息
						if(res2!=undefined){
					if (res2.data.code == 2000) {
						if(res2.data.data!=null){
							let mydata=res2.data.data.records;
							//records
							self.mescroll.endSuccess(mydata.length);
							//设置列表数据
							self.goods = self.goods.concat(mydata); //追加新数据
							setTimeout(() => {
								self.mescroll.endSuccess(mydata.length)
							}, 20)
							
						
							
							//IndexDBCache.addData('databaseName',"buildDetailTable",newdatalist);
							
						
						
						}else{
							self.mescroll.endSuccess(0);
								//设置列表数据
							self.goods = []; //追加新数据
								setTimeout(() => {
							self.mescroll.endSuccess(0)
								}, 20)
							uni.showToast({
								icon: 'none',
								title: "暂无数据"
							});
							self.jieshumethed("");
						}					 
					}else{
						self.mescroll.endSuccess(0);
							//设置列表数据
						self.goods = []; //追加新数据
							setTimeout(() => {
						self.mescroll.endSuccess(0)
							}, 20)
						uni.showToast({
							icon: 'none',
							title: "请求失败"
						});
						self.jieshumethed( "请求失败");
					}
					}else{
						self.mescroll.endSuccess(0);
							//设置列表数据
						self.goods = []; //追加新数据
							setTimeout(() => {
						self.mescroll.endSuccess(0)
							}, 20)
						uni.showToast({
							icon: 'none',
							title: "接口请求超时"
						});
						self.jieshumethed("接口请求超时");
					}
				}).catch(err => {
					self.mescroll.endSuccess(0);
						//设置列表数据
					self.goods = []; //追加新数据
						setTimeout(() => {
					self.mescroll.endSuccess(0)
						}, 20)
				  console.error(err);
					uni.showToast({
						icon: 'none',
						title: err
					});
				 
				  
				});
				
				
				

						
				

			},
			
			requeststo_tool(options){
			 // 在这里可以对请求头进行一些设置
			 let baseUrl = uni.getStorageSync("localurl"); 
			
			console.log(options);
			  options.header = {
			      'Content-Type':'application/json',
			      Authorization: uni.getStorageSync("authorization")
			 }
			
			    return new Promise((resolve, reject) => {
			        uni.request({
			            url: baseUrl + options.url || '',
			            method: options.type || 'POST',
			            data: options.data || {},
			            header: options.header || {}      
			        }).then(data => {
			            let [err, res] = data;        
			            resolve(res);
			        }).catch(error => {
			            reject(error)
			        })
			    });
			}
		}
	}
</script>
